package br.com.esrestaurante.filtro;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.esrestaurante.entidades.Usuario;
import br.com.esrestaurante.tipos.TipoPerfilUsuario;

/**
 * Servlet Filter implementation class LoginFilter
 */
public class LoginFilter implements Filter {

	/**
	 * Default constructor.
	 */
	public LoginFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain filter) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpSession session = request.getSession();

		Usuario usuario;
		Object obj = session.getAttribute("usuarioLogado");
		String url = request.getRequestURI().replaceFirst(request.getContextPath(), "");
		
		
		if(url.contains("css") || url.contains("templates") || url.contains("resource")){
			filter.doFilter(request, response);
		} else {
					
			if(obj != null) {
				usuario = (Usuario) obj;
	
				if(url.contains("/semAcesso.jsf") || url.contains("/login.jsf") || url.contains("/realizarPedido")
							|| url.contains("/inicialCliente")  || url.contains("/selecionarMesa") || url.contains("/consultarCardapio")){
					filter.doFilter(request, response);
				}else if(url.contains("/restaurante/")){
						
						if(TipoPerfilUsuario.GERENTE.equals(usuario.getPerfil())
								|| TipoPerfilUsuario.GARCOM.equals(usuario.getPerfil())) {
							filter.doFilter(request, response);
						}else{
							sendUsuarioSemAcesso(request, response);
						}
					} else if(url.contains("/garcom/")){
						
						if(TipoPerfilUsuario.GERENTE.equals(usuario.getPerfil())
								|| TipoPerfilUsuario.GARCOM.equals(usuario.getPerfil())) {
							filter.doFilter(request, response);
						}else{
							sendUsuarioSemAcesso(request, response);
						}
					} else if(url.contains("/balcao/")){
						
						if(TipoPerfilUsuario.GERENTE.equals(usuario.getPerfil())
								||TipoPerfilUsuario.BALCAO.equals(usuario.getPerfil())) {
							filter.doFilter(request, response);
						}else{
							sendUsuarioSemAcesso(request, response);
						}
					} else if(url.contains("/cozinha/")){
						
						if(TipoPerfilUsuario.GERENTE.equals(usuario.getPerfil())
								|| TipoPerfilUsuario.COZINHA.equals(usuario.getPerfil())) {
							filter.doFilter(request, response);
						}else{
							sendUsuarioSemAcesso(request, response);
						}
					} else  if(url.contains("/cliente/")){
						
						if(TipoPerfilUsuario.GERENTE.equals(usuario.getPerfil())
								|| TipoPerfilUsuario.CLIENTE.equals(usuario.getPerfil())) {
							filter.doFilter(request, response);
						}else{
							sendUsuarioSemAcesso(request, response);
						}
					} else {
						filter.doFilter(request, response);
					}
				
				
				
			}else{ //Usuario nao logado,
				
				if(url.contains("/login.jsf")){
					
					filter.doFilter(request, resp);
				}else{
					//deve fazer o login
					response.sendRedirect(request.getContextPath() + "/login.jsf");
				}
			}
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}
	
	private void sendUsuarioSemAcesso(HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.sendRedirect(request.getContextPath() + "/semAcesso.jsf");
	}

}
